﻿/// <reference path="../typings/jquery/jquery.d.ts" />
/// <reference path="../typings/windjs/windjs.d.ts" />

module User
{
	class UserModel
	{
		public name: string;
		public emails: string;
		public details: string;
		public room: string;
		public phone: string;
		public fax: string;
		public student
	}
	class EditModal
	{

		private modal: JQuery;
		public projectBoard = new Soict.Board();
		public titleBoard = new Soict.Board();
		public roleBoard = new Soict.Board();

		public unparse(): any
		{
			var data = {};
			data['phone'] = $('#editmodel_phone').val();
			data['name']=	$('#editmodel_name').val();
			data['username'] =$('#editmodel_username').val();
			data['password']=$('#editmodel_password').val();
			data['email']=$('#editmodel_email').val();
			data['fax']=	$('#editmodel_fax').val();
			data['avatar']=$('#editmodel_avatar').val();
			data['details']=$('#editmodel_details').val();
			data['room']=$('#editmodel_room').val();
			data['details'] = $('#editmodel_details').val();
			return data;

		}

		public parseData(data: any): void
		{
			$('#editmodel_phone').val(data['phone']);
			$('#editmodel_name').val(data['name']);
			$('#editmodel_username').val(data['username']);
			$('#editmodel_password').val(data['password']);
			$('#editmodel_email').val(data['email']);
			$('#editmodel_fax').val(data['fax']);
			$('#editmodel_avatar').val(data['avatar']);
			$('#editmodel_details').val(data['details']);
			$('#editmodel_room').val(data['room']);
			$('#editmodel_details').val(data['details']);

			var projects = data['Projects'];
			for (var i in projects)
			{
				this.projectBoard.add(projects[i].id, projects[i].description);
			}

			var titles = data['Titles'];
			for (var i in titles)
			{
				this.titleBoard.add(titles[i].tid, titles[i].title1);
			}
			var roles = data['Roles'];
			for (var i in roles)
			{
				this.roleBoard.add(roles[i].id, roles[i].name);
			}

		}

		public init()
		{
			this.roleBoard.init('roleBoard');
			this.modal = $('#editUserModal');
			this.projectBoard.init('projectBoard');
			this.titleBoard.init('titleBoard');
		}

		public show()
		{
			this.modal["foundation"]('reveal', 'open');
		}

		public hide()
		{
			this.modal["foundation"]('reveal', 'close');
		}
	}


	export class Edit
	{
		private editModal = new EditModal();
		private removeModal: JQuery;
		private detalModal: JQuery;
		private waitModal = new Modal.Wait();
		private usernameS: JQuery;
		private username: string;
		private errorModal = new Modal.Error();

		public init(): void
		{

			this.errorModal.init("Kết nối tới server bị ngắt, xin thử lại sau ít phút");
			this.editModal.init();
			this.removeModal = $('#removeModal');
			this.detalModal = $('#detailsModal');
			this.waitModal.init();

			this.usernameS = $('#usernameS');
			var thethis = this;

			$('#editmodel_saveButton').on('click', function ()
			{
				thethis.waitModal.show();
				var datastring = $("#editUserSonForm").parent().serialize();

				console.debug(thethis.editModal.unparse());
				$.post("User/Update", { thethis.editModal.unparse() }, function (data)
				{
					thethis.waitModal.hide();

				}).fail(function ()
				{
					thethis.errorModal.changeMessage("Kết nối tới server bị ngắt, xin thử lại sau ít phút");
					thethis.errorModal.show();
				});
			});


			$('#editmodel_cancelButton').on('click', function ()
			{
				thethis.editModal.hide();
				
			});


			WindJS.wire(this.editModal.projectBoard, "onTagRemove", function (data)
			{
				console.debug(data);
				thethis.waitModal.show();
				$.post("/User/RemoveProject", { pid: data.id, username: thethis.username }, function (s: string)
				{

					if (s == '"Removed Project"')
					{
						thethis.editModal.projectBoard.remove(data.id);
						thethis.editModal.show();
					}
					else
					{
						thethis.errorModal.changeMessage('Không thể xóa được');
						thethis.errorModal.show();
					}
				}).fail(function ()
					{
						thethis.errorModal.changeMessage('Kết nối tới server bị ngắt, xin thử lại sau ít phút nữa');
						thethis.errorModal.show();
					});
				thethis.editModal.show();
			});

			WindJS.wire(this.editModal.titleBoard, "onTagRemove", function (data)
			{
				console.debug(data);
				thethis.waitModal.show();
				$.post("/User/RemoveProject", { tid: data.id, username: thethis.username }, function (s: string)
				{

					if (s == '"Removed Title"')
					{
						thethis.editModal.titleBoard.remove(data.id);
						thethis.editModal.show();
					}
					else
					{
						thethis.errorModal.changeMessage('Không thể xóa được');
						thethis.errorModal.show();
					}
				}).fail(function ()
					{
						thethis.errorModal.changeMessage('Kết nối tới server bị ngắt, xin thử lại sau ít phút nữa');
						thethis.errorModal.show();
					});
				thethis.editModal.show();
			});
			WindJS.wire(this.editModal.roleBoard, "onTagRemove", function (data)
			{
				console.debug(data);
				thethis.waitModal.show();
				$.post("/User/RemoveRole", { rid: data.id, username: thethis.username }, function (s: string)
				{

					if (s == '"Removed Role"')
					{
						thethis.editModal.roleBoard.remove(data.id);
						thethis.editModal.show();
					}
					else
					{
						thethis.errorModal.changeMessage('Không thể xóa được');
						thethis.errorModal.show();
					}
				}).fail(function ()
					{
						thethis.errorModal.changeMessage('Kết nối tới server bị ngắt, xin thử lại sau ít phút nữa');
						thethis.errorModal.show();
					});
				thethis.editModal.show();
			});

			$('#userAddProjectBt').on('click', function ()
			{
				var myWindow = window.open("/Project", "", "width=800,height=600");


				myWindow.onunload = function ()
				{
					if (myWindow['pickedId'] == undefined) return;
					var id = myWindow['pickedId'];
					if (id == "") return;
					thethis.waitModal.show();

					$.post('/User/AddProject', { pid: id, username: thethis.username }, function (data)
					{
						console.log(data);
						var json = JSON.parse(data);
						thethis.editModal.projectBoard.add(json.id, json.description);
						thethis.editModal.show();

					}).fail(function ()
						{
							thethis.waitModal.hide();
							thethis.errorModal.show();
						});

					console.log('id picked = ' + id);

				}


			});

			$('#userAddTitleBt').on('click', function ()
			{
				var myWindow = window.open("/Title", "", "width=800,height=600");


				myWindow.onunload = function ()
				{
					if (myWindow['pickedId'] == undefined) return;
					var id = myWindow['pickedId'];
					if (id == "") return;
					thethis.waitModal.show();

					$.post('/User/AddTitle', { tid: id, username: thethis.username }, function (data)
					{

						var json = JSON.parse(data);
						thethis.editModal.titleBoard.add(json.tid, json.title1);
						thethis.editModal.show();

					}).fail(function ()
						{
							thethis.waitModal.hide();
							thethis.errorModal.show();
						});

					console.log('id picked = ' + id);

				}


			});

			$('#userAddRoleBt').on('click', function ()
			{
				var myWindow = window.open("/Role", "", "width=800,height=600");


				myWindow.onunload = function ()
				{
					if (myWindow['pickedId'] == undefined) return;
					var id = myWindow['pickedId'];
					if (id == "") return;
					thethis.waitModal.show();

					$.post('/User/AddRole', { roleId: id, username: thethis.username }, function (data)
					{
						console.log(data);
						var json = JSON.parse(data);
						thethis.editModal.roleBoard.add(json.id, json.name);
						thethis.editModal.show();

					}).fail(function ()
						{
							thethis.waitModal.hide();
							thethis.errorModal.show();
						});


				}


			});

			$('.removeBt').bind('click', function (data)
			{

				var modelID = this.getAttribute["data-userid"];
			});


			$('.editBt').bind('click', function (data)
			{

				var modelID = this.getAttribute("data-userid");
				thethis.username = modelID;
				thethis.waitModal.show();

				$.post("/User/Get", { id: modelID }, function (data)
				{
					if (data == "") 
					{
						//theo lý thuyết là không bao giờ đi vào nhánh này
						console.error("wrong username");
						setTimeout(function ()
						{
							thethis.waitModal.hide();
						}, 1000);
					}
					var json = JSON.parse(data);
					thethis.usernameS.html(json.name);
					thethis.editModal.parseData(json);
					thethis.editModal.show();
				}).fail(function (xhr, textStatus, errorThrown)
					{
						console.debug(xhr);
						setTimeout(function ()
						{
							thethis.waitModal.hide();
						}, 400);
					});;

			});

			$('.detailsBt').bind('click', function (data)
			{
				var modelID = this.getAttribute("data-userid");
				console.debug((data.srcElement));
			});

		}


	}


}

var user_edit = new User.Edit();
user_edit.init();